package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.mybatis.Pagination;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.orders.OrdersBO;
import com.ymk.bo.stat.StatMchDayBO;
import com.ymk.core.code.cmd.OrdersCmdCode;
import com.ymk.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class OrdersListCmd extends ACmd {


    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId", reqData.getMchId())
                .addParam("status", reqData.getStatus())
                .addParam("payType", reqData.getPayType())
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<OrdersBO> respDataList = this.ordersService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId", sign.getMchId())
                .addParam("memberId", reqData.getMemberId())
                .addParam("loadOrdersMember", true)
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<OrdersBO> respDataList = this.ordersService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeStore(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId", sign.getMchId())
                .addParam("memberId", sign.getUserId())
                .addParam("loadItem", true)
                .setReturnStruct(sign.getReturnStruct());
        Pagination pagination = sign.buildPage();
        pagination.setParams(paramsBuilder.build());
        List<OrdersBO> respDataList = this.ordersService.listForPage(pagination);
        respSign.setPageInfo(pagination);
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public CmdSign executeServer(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        StatMchDayBO  reqData = super.getData(sign, StatMchDayBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mchId",reqData.getMchId())
                .addParam("createDate",reqData.getStatDate())
                .setReturnStruct(sign.getReturnStruct());

        List<OrdersBO> respDataList = this.ordersService.list(paramsBuilder.build());
        respSign.setSource(super.getSource(respDataList));

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return OrdersCmdCode.ORDERS_LIST;
    }

    @Autowired
    private IOrdersService ordersService;
}